﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.IO;
using System.ComponentModel;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base class for all complex value controls - value controls that have custom client-side behavior.
	/// </summary>
	/// <typeparam name="T">The type of the value.</typeparam>
	public abstract class BaseComplexValueControl<T> : BaseValueControl<T>, IComplexControl
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public BaseComplexValueControl()
		{
			Tag = HtmlTextWriterTag.Div;
		}
		#endregion
		#region EVENTS
		/// <summary>
		/// Raised upon initialization on the client.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[Category("Events")]
		public ClientEvent InitControl { get { return this.GetEvent<ClientEvent>("Init"); } }
		#endregion
		#region PROPS
		/// <summary>
		/// Gets the name of the client side class which implements the client side behavior of this control.
		/// </summary>
		[Browsable(false)]
		public virtual string ClientClassName
		{
			get
			{
				return ComplexControlCommon.GetClientClassName(this);
			}
		}
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public override T Value
		{
			get
			{
				return base.Value;
			}
			set
			{
				base.Value = value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region METHODS
		#region INIT
		/// <summary>
		/// Init.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
		}
		/// <summary>
		/// Registers <see cref="Script.BaseComplexValueControl"/>.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			ComplexControlCommon.RegisterIncludes(this, includes);

			includes.Add(Script.BaseComplexValueControl);
		}
		#endregion
		#region RENDER
		protected override void AddValueAttributes(HtmlTextWriter writer)
		{
		}
		/// <summary>
		/// Calls <see cref="AddEventAttributes"/> to add the event attributes.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);

			this.AddEventAttributes(writer);
		}
		/// <summary>
		/// Returns true if the name attribute is required, otherwise false.
		/// </summary>
		/// <returns></returns>
		protected override bool IsNameAttributeRendered()
		{
			return true;
		}
		/// <summary>
		/// Adds attributes for all events that belong to this control.
		/// </summary>
		/// <param name="writer"></param>
		protected virtual void AddEventAttributes(HtmlTextWriter writer)
		{
			ComplexControlCommon.AddEventAttributes(this, writer);
		}
		/// <summary>
		/// Renders initialization script after the HTML markup.
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(HtmlTextWriter writer)
		{
			base.Render(writer);

			//RenderBody(writer);
			RenderInitScript(writer);
		}
		/// <summary>
		/// Renders the initialization script.
		/// </summary>
		/// <param name="writer"></param>
		public virtual void RenderInitScript(HtmlTextWriter writer)
		{
			ComplexControlCommon.RenderInitScript(this, writer);
		}
		/// <summary>
		/// Renders control-specific initialization client script.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="varName"></param>
		public virtual void RenderInitScriptInternal(HtmlTextWriter writer, string varName)
		{
		}
		/// <summary>
		/// Renders control-specific loading client script.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="varName"></param>
		public virtual void RenderLoadScriptInternal(HtmlTextWriter writer, string varName)
		{
			writer.WriteScript("{0}.set_SerializedValue(", varName);
			this.SaveValue(new TA.JSON.JsonWriter(writer));
			writer.WriteScript(");");
		}
		#endregion
		#endregion
		#endregion
	}
}